package com.ruoyi.system.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.MktPeople;
import com.ruoyi.system.domain.MktReward;
import com.ruoyi.system.domain.MktWin;
import com.ruoyi.system.service.IMktRewardService;
import com.ruoyi.system.domain.vo.MktWinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.MktStatisticsMapper;
import com.ruoyi.system.domain.MktStatistics;
import com.ruoyi.system.service.IMktStatisticsService;


@Service
public class MktStatisticsServiceImpl extends ServiceImpl<MktStatisticsMapper, MktStatistics> implements IMktStatisticsService {

    @Autowired
    private IMktRewardService mktRewardService;

    /**
     * 查询营销活动统计列表
     *
     * @param mktStatistics 营销活动统计
     * @return 营销活动统计
     */
    @Override
    public List<MktStatistics> selectMktStatisticsList(MktStatistics mktStatistics) {
        return getBaseMapper().selectMktStatisticsList(mktStatistics);
    }

    @Override
    public List<MktPeople> getById(Long id) {
        return getBaseMapper().selectAllByStatisticsId(id);
    }

    @Override
    public List<MktWinVo> getBySidByPid(Long sId, Long pId) {
        //通过传递过来的统计营销活动的id查询出营销活动对象
        MktStatistics mktStatistics = super.getById(sId);

        List<MktWin> mktWins = getBaseMapper().selectAllByStatisticsIdByPeopleId(sId, pId);
        List<MktWinVo> mktWinVos = mktWins.stream().map(mktWin -> {
            MktWinVo mktWinVo = new MktWinVo();
            mktWinVo.setReward(mktWin.getReward());
            mktWinVo.setStatisticsId(mktWin.getStatisticsId());
            mktWinVo.setId(mktWin.getId());
            mktWinVo.setReceiving(mktWin.getReceiving());
            mktWinVo.setPeopleId(mktWin.getPeopleId());
            mktWinVo.setWinningTime(mktWin.getWinningTime());

            mktWinVo.setStatisticsName(mktStatistics.getName());
            //设置奖励名字,需要根据活动的id来查询他的奖励
            MktReward reward = mktRewardService.getById(mktWin.getReward());
            mktWinVo.setRewardName(reward.getName());
            return mktWinVo;
        }).collect(Collectors.toList());

        return mktWinVos;
    }
}